package Model;

import java.util.Observable;
import java.util.Observer;

/**
 *
 * @author KOK Leng-Steven & RIEU Simon
 */
public class ByStander extends Character implements Observer {

    private boolean byStanderSeeingAvatar=false;
    private boolean avatarSeeingByStander=false;
    private Avatar avatar;
    
    public ByStander(int i, int j, int viewField,Avatar a) 
    {
        super(i, j, viewField);
        this.avatar=a;
        this.avatar.addObserver(this);
        
        this.saluer();
        this.byStanderInAvatarViewField();
    }

    public ByStander(int x, int y) {
        super(x, y);
    }
    
    public boolean isByStanderSeeingAvatar()
    {
        return this.byStanderSeeingAvatar;
    }

    public boolean isAvatarSeeingByStander()
    {
        return this.avatarSeeingByStander;
    }
    
    public void saluer() {
        boolean init=this.isByStanderSeeingAvatar();
        this.AvatarInByStanderViewField();
        if(this.isByStanderSeeingAvatar()==true && init==false)
        {
            System.out.println("Bonjour");
        }
        else if(this.isByStanderSeeingAvatar()==false && init==true)
        {
            System.out.println("Au revoir");
        }
    }

    private void byStanderInAvatarViewField() {
        int posI = this.getI();
        int posJ = this.getJ();
        int mi=this.avatar.getViewField()/2;
        // Si viewFieldInf (=4) est paire on a un champ de vision +2 à droite et -1 à gauche
        // Si viewFieldInf (=5) est impaire on a un champ de vision +2 à droite et -2 à gauche
        int viewInfI = this.avatar.getI() - mi - (this.avatar.getViewField() % 2);
        int viewSupI = this.avatar.getI() + mi;
        int viewInfJ = this.avatar.getJ() - mi - (this.avatar.getViewField() % 2);
        int viewSupJ = this.avatar.getJ() + mi;

        if (posI > this.max(viewInfI, -1) && posI <= this.min(viewSupI, 20)) 
        {
            if (posJ > this.max(viewInfJ, -1) && posJ <= this.min(viewSupJ, 20)) {
                this.avatarSeeingByStander = true;
            }
            else
            {this.avatarSeeingByStander = false;}
        }
        else
        {this.avatarSeeingByStander = false;}
    }

    private void AvatarInByStanderViewField()
    {
        int posI=this.avatar.getI();
        int posJ=this.avatar.getJ();
        int mi=this.getViewField()/2;
        int viewInfI = this.getI()-mi-(this.getViewField()%2);
        int viewSupI = this.getI()+mi;
        int viewInfJ = this.getJ()-mi-(this.getViewField()%2);
        int viewSupJ = this.getJ()+mi;
        if(posI>this.max(viewInfI,-1) && posI<=this.min(viewSupI,20))
        {
            if(posJ>this.max(viewInfJ,-1) && posJ<=this.min(viewSupJ,20))
            {
                this.byStanderSeeingAvatar=true;
            }
            else
            {this.byStanderSeeingAvatar = false;}
        }
        else
        {this.byStanderSeeingAvatar = false;}
    }
    
    /**
     * Méthode retournant le minimum entre deux nombres
     * @param a
     * @param b
     * @return 
     */
    private int min(int a, int b) 
    {
        if (a > b) {
            return b;
        } else {
            return a;
        }
    }

    /**
     * Méthode retournant le maximum entre deux nombres
     * @param a
     * @param b
     * @return 
     */
    private int max(int a, int b) 
    {
        if (a > b) {
            return a;
        } else {
            return b;
        }
    }
    
    @Override
    public void update(Observable o, Object arg) {
        if (o instanceof Avatar) {
            byStanderInAvatarViewField();
            this.saluer();
        }
    }
    
    @Override
    public boolean equals(Object other) {
        boolean result;
        if ((other == null) || (getClass() != other.getClass())) {
            result = false;
        } // end if
        else {
            ByStander otherByStander = (ByStander) other;
            result = (this.i == otherByStander.i) && (j == otherByStander.j);
        } // end else

        return result;
    } // end equals
 
}
